package cate.game.admin.menu.rpc;

import cate.game.GameBody;
import cate.game.admin.menu.MenuSetup;
import cate.game.role.bag.hero.HeroBag;
import cp.solution.rpc.path.RpcTestPath;
import cp.solution.rpc.test.TheCat;
import cp.solution.rpc.test.TheDog;
import easy.java.runtime.JvmInfo;
import easy.java.struct.ref.IntRef;
import fax.common.util.ThreadUtils;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class RpcMenu extends MenuSetup {

    public RpcMenu(GameBody game) {
        super("RPC");

        reg(game, "跨服:发送 阻塞消息", 1, "RR_BLOCK_KRYO", (role, params) -> {
            int blockSec = toInt(params, 0, 15);
            log.info("准备发送阻塞消息给跨服, 跨服端处理耗时将超过{}秒", blockSec);
            new Thread(() -> {
                IntRef reply = game.rpcSend().requestResponseCross(RpcTestPath.RR_BLOCK_KRYO, new IntRef(blockSec), IntRef.class);
                log.info("跨服回复:{}", reply);
            }).start();
        });

        reg(game, "跨服:发送 许多小数据(kryo)", 2, "RR_MANY_SMALL_DATA_KRYO 参数1：每线程执行次数, 参数2：线程数", (role, params) -> {
            final int batchTimes = toInt(params, 0, 200);
            final int nt = toInt(params, 1, 10);
            long total = nt * batchTimes;
            Thread[] ts = new Thread[nt];
            for(int i = 0; i < nt; i++) {
                Thread t1 = new Thread(new RpcQpsTestTask("QT"+i, game.rpcSend(), batchTimes));
                ts[i] = t1;
            }

            long beginTime = System.currentTimeMillis();

            for(int i = 0; i < nt; i++) {
                ts[i].start();
            }

            for(int i = 0; i < nt; i++) {
                ts[i].join();
            }

            long costTime = System.currentTimeMillis() - beginTime;
            long kpTime = (costTime*1000L/total);
            log.info("全部完成 总耗时= {}ms, 每千个耗时= {}ms", costTime, kpTime);
            game.notice.message(role, "全部完成 总耗时= "+costTime+"ms, 每千个耗时= "+kpTime+"ms");

            new Thread(() -> {
                ThreadUtils.sleep(300);
                System.gc();
                ThreadUtils.sleep(500);
                JvmInfo ji = new JvmInfo();
                ji.load();
                ji.printGarbageCollectorInfo();
                ji.printMemoryInfo();
            }).start();
        });

        //--------------------------------------------------------------

        reg(game, "跨服:RR_CAT_KRYO_2_DOG_KRYO", 0, "", (role, params) -> {
            TheCat cat = new TheCat();
            cat.name = "阿喵.kryo";
            cat.age = 1;
            TheDog dog = game.rpcSend().requestResponseCross(RpcTestPath.RR_CAT_KRYO_2_DOG_KRYO, cat, TheDog.class);
            log.info("跨服回复:{}", dog);
        });

        reg(game, "组服:RR_CAT_KRYO_2_DOG_KRYO", 0, "", (role, params) -> {
            TheCat cat = new TheCat();
            cat.name = "阿喵.kryo";
            cat.age = 1;
            TheDog dog = game.rpcSend().requestResponseGroup(RpcTestPath.RR_CAT_KRYO_2_DOG_KRYO, cat, TheDog.class);
            log.info("组服回复:{}", dog);
        });

        //--------------------------------------------------------------

        reg(game, "跨服:RR_CAT_JSON_2_DOG_JSON", 0, "", (role, params) -> {
            TheCat cat = new TheCat();
            cat.name = "阿喵.json";
            cat.age = 2;
            TheDog dog = game.rpcSend().requestResponseCross(RpcTestPath.RR_CAT_JSON_2_DOG_JSON, cat, TheDog.class);
            log.info("跨服回复:{}", dog);
        });

        reg(game, "组服:RR_CAT_JSON_2_DOG_JSON", 0, "", (role, params) -> {
            TheCat cat = new TheCat();
            cat.name = "阿喵.json";
            cat.age = 2;
            TheDog dog = game.rpcSend().requestResponseGroup(RpcTestPath.RR_CAT_JSON_2_DOG_JSON, cat, TheDog.class);
            log.info("组服回复:{}", dog);
        });

        //--------------------------------------------------------------

        reg(game, "跨服:RR_CAT_KRYO_2_DOG_JSON", 0, "", (role, params) -> {
            TheCat cat = new TheCat();
            cat.name = "阿喵.kryo";
            cat.age = 3;
            TheDog dog = game.rpcSend().requestResponseCross(RpcTestPath.RR_CAT_KRYO_2_DOG_JSON, cat, TheDog.class);
            log.info("跨服回复:{}", dog);
        });

        reg(game, "组服:RR_CAT_KRYO_2_DOG_JSON", 0, "", (role, params) -> {
            TheCat cat = new TheCat();
            cat.name = "阿喵.kryo";
            cat.age = 3;
            TheDog dog = game.rpcSend().requestResponseGroup(RpcTestPath.RR_CAT_KRYO_2_DOG_JSON, cat, TheDog.class);
            log.info("组服回复:{}", dog);
        });

        //--------------------------------------------------------------

        reg(game, "跨服:RR_CAT_JSON_2_DOG_KRYO", 0, "", (role, params) -> {
            TheCat cat = new TheCat();
            cat.name = "阿喵.json";
            cat.age = 4;
            TheDog dog = game.rpcSend().requestResponseCross(RpcTestPath.RR_CAT_JSON_2_DOG_KRYO, cat, TheDog.class);
            log.info("跨服回复:{}", dog);
        });

        reg(game, "组服:RR_CAT_JSON_2_DOG_KRYO", 0, "", (role, params) -> {
            TheCat cat = new TheCat();
            cat.name = "阿喵.json";
            cat.age = 4;
            TheDog dog = game.rpcSend().requestResponseGroup(RpcTestPath.RR_CAT_JSON_2_DOG_KRYO, cat, TheDog.class);
            log.info("组服回复:{}", dog);
        });

        //--------------------------------------------------------------

        reg(game, "跨服:RR_TEXT_2_DOG_JSON", 0, "", (role, params) -> {
            TheDog dog = game.rpcSend().requestResponseCross(RpcTestPath.RR_TEXT_2_DOG_JSON, "阿喵.text", TheDog.class);
            log.info("跨服回复:{}", dog);
        });

        reg(game, "组服:RR_TEXT_2_DOG_JSON", 0, "", (role, params) -> {
            TheDog dog = game.rpcSend().requestResponseGroup(RpcTestPath.RR_TEXT_2_DOG_JSON, "阿喵.text", TheDog.class);
            log.info("组服回复:{}", dog);
        });

        //--------------------------------------------------------------

        reg(game, "跨服:RR_CAT_JSON_2_TEXT", 0, "", (role, params) -> {
            TheCat cat = new TheCat();
            cat.name = "阿喵.json";
            cat.age = 6;
            String text = game.rpcSend().requestResponseCross(RpcTestPath.RR_CAT_JSON_2_TEXT, cat, String.class);
            log.info("跨服回复:{}", text);
        });

        reg(game, "组服:RR_CAT_JSON_2_TEXT", 0, "", (role, params) -> {
            TheCat cat = new TheCat();
            cat.name = "阿喵.json";
            cat.age = 6;
            String text = game.rpcSend().requestResponseGroup(RpcTestPath.RR_CAT_JSON_2_TEXT, cat, String.class);
            log.info("组服回复:{}", text);
        });

        //--------------------------------------------------------------

        reg(game, "跨服:FAF_PLAY_SIDE_KRYO", 0, "", (role, params) -> {
            HeroBag heroBag = role.getBag().hero;
            game.rpcSend().fireAndForgetCross(RpcTestPath.FAF_PLAY_SIDE_KRYO, heroBag);
            log.info("已发送给跨服: {}", heroBag);
        });

        reg(game, "组服:FAF_PLAY_SIDE_KRYO", 0, "", (role, params) -> {
            HeroBag heroBag = role.getBag().hero;
            game.rpcSend().fireAndForgetGroup(RpcTestPath.FAF_PLAY_SIDE_KRYO, heroBag);
            log.info("已发送给组服: {}", heroBag);
        });

        //--------------------------------------------------------------
    }
}